var validateSubmitOnce = true; //用于限制验证字段时只提交一次请求,当要验证的字段改变时该值设为true,避免只能验证一次

/**
 * 将请求地址加上时间戳，防止浏览器缓存
 */
function getTimeURL(url){	
	if(url.indexOf("?") == -1){
		url += "?timeStamp=" + new Date().getTime();
	}else{
		url += "&timeStamp=" + new Date().getTime();
	}
	return encodeURI(url);
}

/**
 * 通用combobox
 */
function combobox_1(id,url,editable){
	$("#" + id).combobox({   
	    url:url,
	    valueField:'id',
	    textField:'text',
	    editable:editable
	});
}

/**
 * 通用combobox
 */
function combobox_2(className,url,editable){
	$("." + className).each(function(){
		$(this).combobox({   
		    url:url,   
		    valueField:'id',
		    textField:'text',
		    editable:editable
		});	
	});
}

/**
 * 动态下拉框默认选中第一个
 */
function combobox_select_frist(id,url,editable,selectValue){
	if(selectValue==""){
		$.ajax({
			type:"post",
			url:url,
			dataType:"json",
			success:function(data){
				$("#" + id).combobox({   
				    url:url,
				    valueField:'id',
				    textField:'text',
				    editable:editable
				}).combobox("select",data[0].id);
			}
		});
		
	}else{
		$("#" + id).combobox({   
		    url:url,
		    valueField:'id',
		    textField:'text',
		    editable:editable		   
		}).combobox("select",selectValue);
	}
}
/**
 *  combobox 动态下拉框默认选中第一个，添加选中事件
 * @param id
 * @param url 
 * @param editable  是否可编辑
 * @param selectValue	默认选中值------undefined/null/''时默认选中第一个
 * @param changeEventFn	值改变时事件
 * @param selectEventFn	选中时事件
 */
function combobox_select_event(id,url,editable,selectValue,changeEventFn,selectEventFn){
	var option = {   
	    url:url,
	    valueField:'id',
	    textField:'text',
	    editable:editable
	}
	if(changeEventFn) option.onChange = changeEventFn;
	if(selectEventFn) option.onSelect = selectEventFn;
	if(selectValue == undefined || selectValue == "" || selectValue == null){
		option.onLoadSuccess=function (){
	        var data = $(this).combobox('getData');
	        if (data.length > 0) {
	            $(this).combobox("select",data[0].id);
	        }
		};
	    $("#" + id).combobox(option);
	}else{
		$("#" + id).combobox(option).combobox("select",selectValue);
	}
}

/**
 * easyui 数字combobox
 * @param elementId
 * @param selectValue
 * @param numArray
 */
function numCombobox(elementId,selectValue,prec,numArray){
	if(numArray == undefined){		
		numArray = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20];
	}
	var data = "["; 
	var i=0;
	for(;i<numArray.length-1;i++){		
		data += '{"id": ' + numArray[i].toFixed(prec) + ', "text": "' + numArray[i].toFixed(prec) + '"},';		
	}
	data += '{"id": ' + numArray[i].toFixed(prec) + ', "text": "' + numArray[i].toFixed(prec) + '"}]';	
	$("#" + elementId + "Select").combobox({
		data: eval(data),
		valueField:'id',
		textField:'text',
		onSelect:function(data){
			$("#" + elementId).val(data.text);
			$("#" + elementId).focus();
			$("#" + elementId).blur();
		},
		onChange:function(data){			
			$("#" + elementId).val(data);
			$("#" + elementId).focus();
			$("#" + elementId).blur();
		}
	}).combobox("select",selectValue);
}

/**
 * easyui 数字combobox
 * @param elementId
 * @param selectValue
 * @param numArray
 */
function numCombobox_2(elementId,selectValue,prec,num){
	var numArray = null;
	if(num == undefined){		
		numArray = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20];
	}else{
		numArray = new Array();
		for(var i=0;i<num;i++){
			numArray[i] = i+1;
		}
	}
	var data = "["; 
	var i=0;
	for(;i<numArray.length-1;i++){		
		data += '{"id": ' + numArray[i].toFixed(prec) + ', "text": "' + numArray[i].toFixed(prec) + '"},';		
	}
	data += '{"id": ' + numArray[i].toFixed(prec) + ', "text": "' + numArray[i].toFixed(prec) + '"}]';	
	$("#" + elementId + "Select").combobox({
		data: eval(data),
		valueField:'id',
		textField:'text',
		onSelect:function(data){
			$("#" + elementId).val(data.text);
			$("#" + elementId).focus();
			$("#" + elementId).blur();
		},
		onChange:function(data){			
			$("#" + elementId).val(data);
			$("#" + elementId).focus();
			$("#" + elementId).blur();
		}
	}).combobox("select",selectValue);
}


/**
 * 回车键按下时击发事件
 */
function enterKeyDown(id,fn){	
	$("#" + id).keydown(function(event){
		event = event || window.event;
		if(event.keyCode==13){
			fn();
		}			
	});
}

/**
 * 通过uploadify上传单个文件
 */
function uploadOneFile(id,url,fileSizeLimit,width,height,uploadMessage,fileSavePath,fileSaveName,imgId){	
	$("#" + id).uploadify({
		'swf':'/mrjy/common/uploadify/uploadify.swf',
		'uploader': url,		
		'progressData':'speed',
		'fileObjName':'uploadFile', //该属性必须与Controller中的File类型属性相对应
		'removeCompleted':true,
		'multi':false,
		'auto':true,
		'buttonImage':'/mrjy/common/uploadify/button_image_1.png',
		'width':width,
		'height':height,
		'fileSizeLimit':fileSizeLimit,		
        'formData':{'fileSavePath' : fileSavePath,'fileSaveName':$('#' + fileSaveName).val()},
        'onDialogOpen' : function() {        	
        	$("#" + uploadMessage).html("");
        },
        'onSelectError' : function(file,errorCode,errorMsg) {       
        	console.log(errorCode+errorMsg);
        	if(errorCode == -110){        		
        		$("#" + uploadMessage).html("<img src='/mrjy/common/images/error.gif' style='vertical-align: middle;'/><font color='red'>" + file.name + "超过上传上限（" + fileSizeLimit + "），上传失败!</font>");
        	}
        },        
        'onUploadSuccess': function (file, data, response){      	
        	var data = data.split("_");
         	if(data[0] == "ok"){
         		$("#" + fileSaveName).val(data[1]);
         		$("#" + uploadMessage).html("<img src='/mrjy/common/images/success.gif' style='vertical-align: middle;'/><font color='green'>文件上传成功！</font>");
         		$("#" + uploadMessage).fadeIn(1000).fadeOut(3000,function(){$("#" + uploadMessage).html('');});	
         		$('#' + id).uploadify('destroy');
         		$("#" + fileSaveName).click();
         		
         		if(imgId==null){       			
         		}else{       		
         			$("#" + imgId).attr("src",mrjyFileUrl+fileSavePath+"/"+data[1]+"?"+new Date().getTime());
         		}
         	}
         }
	});	
}

/**
 * 表单元素格式验证 
 */
function baseFormValidator(elementId,elementName,minLength,maxLength,regexFlag){	
	var onshowStr = "请输入" + elementName;
	var onfocusStr;
	if(regexFlag == "chinese"){
		onfocusStr = elementName + "由" + parseInt(minLength/2) + "~" + parseInt(maxLength/2) + "个";
	}else{
		onfocusStr = elementName + "由" + minLength + "~" + maxLength + "个";
	}
	
	var oncorrectStr = elementName + "输入正确！";
	var emptyStr = {leftEmpty:false,rightEmpty:false,emptyError: elementName + "两边不能输入空格"};
	var onerrorStr =  elementName + "由";
	var regexStr = "";
	if(regexFlag == "ascii"){
		regexStr = "英文符号";
	}else if(regexFlag == "username"){
		regexStr = "字母、数字";					
	}else if(regexFlag == "chinese"){
		regexStr = "汉字";					
	}else if(regexFlag == "letter"){
		regexStr = "英文字母";				
	}else if(regexFlag == "time"){
		onfocusStr = elementName + "由00:00:00格式";	
		onerrorStr = onfocusStr;
	}else if(regexFlag == "mobile"){	
		regexStr = "纯数字";		
	}else{
		regexStr = "中英文字符";
	}	
	regexStr += "组成";
	if(regexFlag == undefined){
		$("#" + elementId).formValidator({onShow: onshowStr,onFocus: onfocusStr + regexStr, onCorrect: oncorrectStr})
		.inputValidator({min: minLength,max: maxLength,empty: emptyStr,	onError: onfocusStr + regexStr});
	}else{
		$("#" + elementId).formValidator({onShow: onshowStr,onFocus: onfocusStr + regexStr, onCorrect: oncorrectStr})
		.inputValidator({min: minLength,max: maxLength,empty: emptyStr,	onError: onfocusStr + regexStr})
		.regexValidator({regExp: regexFlag,dataType:"enum",onError: onerrorStr + regexStr});
	}	
}


/**
 * 表单元素AJAX逻辑验证
 */
function ajaxFormValidator(elementId,elementName,minLength,maxLength,ajaxUrl,regexFlag){
	var onshowStr = "请输入" + elementName;
	var onfocusStr = elementName + "由" + minLength + "~" + maxLength + "个";
	var oncorrectStr = "恭喜，该" + elementName + "可用！";
	var emptyStr = {leftEmpty:false,rightEmpty:false,emptyError: elementName + "两边不能输入空格"};
	var onerrorStr =  elementName + "由";
	var regexStr = "";
	if(regexFlag == "ascii"){
		regexStr = "英文符号";
	}else if(regexFlag == "username"){
		regexStr = "字母、数字";					
	}else if(regexFlag == "chinese"){
		regexStr = "汉字";					
	}else if(regexFlag == "letter"){
		regexStr = "英文字母";				
	}else if(regexFlag == "time"){
		onfocusStr = elementName + "由00:00:00格式";	
		onerrorStr = onfocusStr;
	}else if(regexFlag == "mobile"){	
		regexStr = "纯数字";
	}else{	
		regexStr = "中英文字符";		
	}	
	regexStr += "组成";
	if(regexFlag == undefined){
		$("#" + elementId).formValidator({onShow: onshowStr,onFocus: onfocusStr + regexStr, onCorrect: oncorrectStr})
		.inputValidator({min: minLength,max: maxLength,empty: emptyStr,	onError: onfocusStr + regexStr})
		.ajaxValidator({type:"get",url:ajaxUrl,dataType:"json",beforeSend:function(){if(validateSubmitOnce==true){validateSubmitOnce=false;return true;}else{return false;}},success:function(data){if(data=="ok"){return true;}else{return false;}},error:function(){alert("服务器连接错误，请重试...");},onError:"该" + elementName + "已被使用，请重输！",onWait:"正在校验，请稍候..."});
	}else{
		$("#" + elementId).formValidator({onShow: onshowStr,onFocus: onfocusStr + regexStr, onCorrect: oncorrectStr})
		.inputValidator({min: minLength,max: maxLength,empty: emptyStr,	onError: onfocusStr + regexStr})
		.regexValidator({regExp: regexFlag,dataType:"enum",onError: onerrorStr + regexStr})
		.ajaxValidator({type:"get",url:ajaxUrl,dataType:"json",beforeSend:function(){
			if(validateSubmitOnce==true){
				validateSubmitOnce=false;
				return true;
			}else{
				return false;
			}
		},success:function(data){if(data=="ok"){return true;}else{return false;}},error:function(){alert("服务器连接错误，请重试...");},onError:"该" + elementName + "已被使用，请重输！",onWait:"正在校验，请稍候..."});
	}
}

/**
 * 表单元素conbox AJAX逻辑验证
 */
function ajaxFormValidator_conbox(elementId,elementName,minLength,ajaxUrl){
	var onshowStr = "请选择" + elementName;
	var oncorrectStr =  elementName + "选择成功！";
	var emptyStr = {leftEmpty:false,rightEmpty:false,emptyError:onshowStr };
	
	$(elementId).formValidator({onShow: onshowStr,onFocus: onshowStr, onCorrect: oncorrectStr})
	.inputValidator({min: minLength,type:"number",empty: emptyStr,	onError: onshowStr})
	.regexValidator({regExp: "intege1",dataType:"enum",onError: onshowStr})
	.ajaxValidator({type:"get",url:ajaxUrl,dataType:"json",beforeSend:function(){if(validateSubmitOnce==true){validateSubmitOnce=false;return true;}else{return false;}},success:function(data){if(data=="ok"){return true;}else{return false;}},error:function(){alert("服务器连接错误，请重试...");},onError:"该" + elementName + "选择不合法，请重选！",onWait:"正在校验，请稍候..."});
}

/**
 * 数字验证
 */
function numFormValidator(elementId,elementName,minLength,maxLength,regexFlag,ajaxUrl){	
	var onshowStr = "请输入" + elementName;
	var onfocusStr = elementName + "由" + minLength + "~" + maxLength + "之间的";
	var oncorrectStr = elementName + "输入正确！";
	var emptyStr = {leftEmpty:false,rightEmpty:false,emptyError: elementName + "两边不能输入空格"};
	var onerrorStr =  elementName + "由";
	var regexStr = "";
	if(regexFlag == "intege1"){
		regexStr = "正整数";	
	}else if(regexFlag == "decmal1"){
		regexStr = "正浮点数";	
	}else if(regexFlag == "decmal6"){
		regexStr = "1位小数";	
	}else if(regexFlag=="decmal4"){
		regexStr = "非负小数";
	}	
	regexStr += "组成";	
	if(ajaxUrl == undefined){
		$("#" + elementId).formValidator({onShow: onshowStr,onFocus: onfocusStr + regexStr, onCorrect: oncorrectStr})
			.inputValidator({min: minLength,max: maxLength,type:"number",empty: emptyStr,	onError: onfocusStr + regexStr})
			.regexValidator({regExp: regexFlag,dataType:"enum",onError: onerrorStr + regexStr});
	}else{
		$("#" + elementId).formValidator({onShow: onshowStr,onFocus: onfocusStr + regexStr, onCorrect: oncorrectStr})
			.inputValidator({min: minLength,max: maxLength,type:"number",empty: emptyStr,	onError: onfocusStr + regexStr})
			.regexValidator({regExp: regexFlag,dataType:"enum",onError: onerrorStr + regexStr})
			.ajaxValidator({type:"get",url:ajaxUrl,dataType:"json",beforeSend:function(){if(validateSubmitOnce==true){validateSubmitOnce=false;return true;}else{return false;}},success:function(data){if(data=="ok"){return true;}else{return false;}},error:function(){alert("服务器连接错误，请重试...");},onError:"该" + elementName + "已被使用，请重输！",onWait:"正在校验，请稍候..."});
	}
}

/**
 * Select元素AJAX逻辑验证
 */
function selectFormValidator(elementId,elementName,ajaxUrl){
	var onshowStr = "请选择" + elementName;	
	var oncorrectStr = "恭喜，该" + elementName + "可用！";	
	if(ajaxUrl == undefined){
		$("#" + elementId).formValidator({onShow: onshowStr,onFocus: onshowStr, onCorrect: oncorrectStr});
	}else{
		$("#" + elementId).formValidator({onShow: onshowStr,onFocus: onshowStr, onCorrect: oncorrectStr})
		.ajaxValidator({type:"get",url:ajaxUrl,dataType:"json",beforeSend:function(){if(validateSubmitOnce==true){validateSubmitOnce=false;return true;}else{return false;}},success:function(data){if(data=="ok"){return true;}else{return false;}},error:function(){alert("服务器连接错误，请重试...");},onError:"该" + elementName + "已被使用，请重新选择！",onWait:"正在校验，请稍候..."});
	}
}

/**
 * 点击按钮弹出图片
 */
function viewPic(url){
	if($("#msw_pic_window").length==0){	
		$("body").append("<div id='msw_pic_window'></div>");
	}	
	$("#msw_pic_window").html("<img src='" + url + "' width='400' height='300'/>");
	$('#msw_pic_window').window({
		title : "查看图片",
		modal : true,
		width : 414,
		height : 336,
		shadow : false,
		closed : false,
		minimizable : false,
		maximizable : false,
		collapsible : false,
		iconCls : 'icon-search',
		resizable : false,
		top:($(window).height() - 350) * 0.5,
		left:($(window).width() - 450) * 0.5
	});	
}
//校验手机号码
function validMobilePhone(mobilePhone){
	var flag = true ;
	var tel = /^(130|131|132|133|134|135|136|137|138|139|147|150|151|152|153|154|155|156|157|158|159|170|177|176|180|181|182|183|184|185|186|187|188|189)\d{8}$/;
	//验证手机号码是否正确
    if(!mobilePhone || !tel.test(mobilePhone)){
    	//weui.topTips('请输入正确的手机号码');
    	flag = false;
    }
	
	return flag;
}

/**
 * 通过uploadify上传单个文件,其中参数中的width和height分别是照片的宽高
 */
function uploadCoverImg(id,url,fileSizeLimit,uploadMessage,fileSavePath,fileSaveName,imgId){	
	$("#" + id).uploadify({
		'swf':'/mrjy/common/uploadify/uploadify.swf',
		'uploader': url,		
		'progressData':'speed',
		'fileObjName':'uploadFile', //该属性必须与Controller中的File类型属性相对应
		'removeCompleted':true,
		'multi':false,
		'auto':true,
		'buttonImage':'/mrjy/common/uploadify/button_image_1.png',
		'width':100,
		'height':30,
		'fileSizeLimit':fileSizeLimit,		
        'formData':{'fileSavePath' : fileSavePath,'fileSaveName':$('#' + fileSaveName).val(),'width':750,'height':375},
        'onDialogOpen' : function() {        	
        	$("#" + uploadMessage).html("");
        },
        'onSelectError' : function(file,errorCode,errorMsg) {       
        	console.log(errorCode+errorMsg);
        	if(errorCode == -110){        		
        		$("#" + uploadMessage).html("<img src='/mrjy/common/images/error.gif' style='vertical-align: middle;'/><font color='red'>" + file.name + "超过上传上限（" + fileSizeLimit + "），上传失败!</font>");
        	}
        },        
        'onUploadSuccess': function (file, data, response){      	
        	var data = data.split("_");
         	if(data[0] == "ok"){
         		$("#" + fileSaveName).val(data[1]);
         		$("#" + uploadMessage).html("<img src='/mrjy/common/images/success.gif' style='vertical-align: middle;'/><font color='green'>文件上传成功！</font>");
         		$("#" + uploadMessage).fadeIn(1000).fadeOut(3000,function(){$("#" + uploadMessage).html('');});	
         		$('#' + id).uploadify('destroy');
         		$("#" + fileSaveName).click();
         		
         		if(imgId==null){       			
         		}else{       		
         			$("#" + imgId).attr("src",mrjyFileUrl+fileSavePath+"/"+data[1]+"?"+new Date().getTime());
         		}
         	}
         }
	});	
}